package name.jautzy.esi.client;

import java.util.Date;

import name.jautzy.esi.client.model.EsiMessages;
import name.jautzy.esi.client.model.LoginInfo;
import name.jautzy.esi.client.service.EveDatabaseService;
import name.jautzy.esi.client.service.EveDatabaseServiceAsync;
import name.jautzy.esi.client.service.FitService;
import name.jautzy.esi.client.service.FitServiceAsync;
import name.jautzy.esi.client.service.LoginService;
import name.jautzy.esi.client.service.LoginServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootPanel;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Esi implements EntryPoint {
	private LoginInfo loginInfo = null;
	private static Anchor signInLink = new Anchor("Sign In");
	private static Anchor signOutLink = new Anchor("Sign Out");
	private static Esi esi = null;
	private final EveDatabaseServiceAsync dbservice = (EveDatabaseServiceAsync)GWT.create(EveDatabaseService.class);
	private final FitServiceAsync fitservice = (FitServiceAsync)GWT.create(FitService.class);
	private final LoginServiceAsync loginService = (LoginServiceAsync)GWT.create(LoginService.class);
	private ListStore<EsiMessages> messages;
	public final static String version = "0.6";
	public final static String ccpCopyright = "EVE Online and the EVE logo are the registered trademarks of CCP hf. All rights are reserved worldwide. All other trademarks are the property of their respective owners. EVE Online, the EVE logo, EVE and all associated logos and designs are the intellectual property of CCP hf. All artwork, screenshots, characters, vehicles, storylines, world facts or other recognizable features of the intellectual property relating to these trademarks are likewise the intellectual property of CCP hf. CCP hf. has granted permission to Eve Ship Identity to use EVE Online and all associated logos and designs for promotional and information purposes on its website but does not endorse, and is not in any way affiliated with, Eve Ship Identity. CCP is in no way responsible for the content on or functioning of this website, nor can it be liable for any damage arising from the use of this website.";
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		esi = this;
		messages = new ListStore<EsiMessages>();
		
		String url = GWT.getHostPageBaseURL();
		if (url.contains("127.0.0.1"))
			url = new String("http://127.0.0.1:8888/Esi.html?gwt.codesvr=127.0.0.1:9997");
		
		loginService.login(url, new AsyncCallback<LoginInfo>() {
			public void onFailure(Throwable error) {
			}

			public void onSuccess(LoginInfo result) {
				loginInfo = result;
				signOutLink.setHref(loginInfo.getLogoutUrl());
				if(loginInfo.isLoggedIn()) {
					loadApp();
				} else {
					loadLogin();
				}
			}
		});

	}

	private void loadApp() {
		messages.removeAll();
		addMessage("App loaded");
		EsiLayoutContainer root = new EsiLayoutContainer();
		Viewport vp = new Viewport();
		vp.setLayout(new FitLayout());
		vp.add(root);
		RootPanel.get().add(vp);
	}

	private void loadLogin() {
		signInLink.setHref(loginInfo.getLoginUrl());
		
		Viewport vp = new Viewport();
		vp.setLayout(new CenterLayout());
		
		ContentPanel loginPanel = new ContentPanel();
		loginPanel.setLayout(new VBoxLayout());
		loginPanel.setHeading("Login");
		loginPanel.setFrame(true);
		loginPanel.setSize(150,100);
		loginPanel.add(new Label("Please sign in to your google account to access Eve Ship Identity"));
        loginPanel.add(signInLink);
        vp.add(loginPanel);
		RootPanel.get().add(vp);
	}
	
// Static Methods
	public static Anchor getSignInLink () {
		return signInLink;
	}
	
	public static Anchor getSignOutLink () {
		return signOutLink;
	}
	
	
	public static Esi getEsi() {
		return esi;
	}
	
// Utility Methods
	public LoginInfo getLoginInfo() {
		return loginInfo;
	}
	
	public EveDatabaseServiceAsync getDatabaseService() {
		return dbservice;
	}
	
	public FitServiceAsync getFitService() {
		return fitservice;
	}
	
	public LoginServiceAsync getLoginService() {
		return loginService;
	}
	
	public ListStore<EsiMessages> getMessages() {
		return messages;
	}
	
	public void addMessage(String message) {
		messages.add(new EsiMessages(new Date(),message));
	}
	
}
